package com.atguigu.gulimall.gateway.myuntil;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AESUtil1 {
    public static final String AES_CBC_PKCS_5_PADDING = "AES/CBC/PKCS5Padding";
    public static final String AES = "AES";

    /**
     *
     * @param key 密码
     * @param iv 向量
     * @param clearText 明文
     * @return base64的密文
     * @throws Exception 很多异常
     */
    public static String encrypt(String key, String iv, String clearText) throws Exception {
        byte[] rawKey = key.getBytes();
        byte[] result = encrypt(rawKey, iv.getBytes(), clearText.getBytes());
        return Base64.encodeBase64String(result);
    }
    /**
     *
     * @param keyBytes 密码文节数组
     * @param iv 向量字节数组
     * @param clearTextBytes 明文字节数组
     * @return 密文字节数组
     * @throws Exception 加密时的异常
     */
    private static byte[] encrypt(byte[] keyBytes, byte[] iv, byte[] clearTextBytes) throws Exception {
        SecretKeySpec keySpec = new SecretKeySpec(keyBytes, AES);
        Cipher cipher = Cipher.getInstance(AES_CBC_PKCS_5_PADDING);
        IvParameterSpec ivs = new IvParameterSpec(iv);
        cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivs);
        return cipher.doFinal(clearTextBytes);
    }

    /**
     *
     * @param key 密码
     * @param iv 向量
     * @param encrypted 密文
     * @return 明文
     * @throws Exception 很多异常
     */
    public static String decrypt(String key, String iv, String encrypted) throws Exception {
        byte[] rawKey = key.getBytes();
        byte[] enc = Base64.decodeBase64(encrypted);
        byte[] result = decrypt(rawKey, iv.getBytes(), enc);
        return new String(result);
    }

    /**
     *
     * @param key 密码字节数组
     * @param iv 向量字节数组
     * @param encrypted 密文字节数组
     * @return 明文字节数组
     * @throws Exception 解密异常
     */
    private static byte[] decrypt(byte[] key, byte[] iv, byte[] encrypted) throws Exception {
        SecretKeySpec keySpec = new SecretKeySpec(key, AES);
        Cipher cipher = Cipher.getInstance(AES_CBC_PKCS_5_PADDING);
        IvParameterSpec ivs = new IvParameterSpec(iv);
        cipher.init(Cipher.DECRYPT_MODE, keySpec, ivs);
        return cipher.doFinal(encrypted);
    }

    public static void main(String[] args) {
        String content = "success";
        String password = "htznhhakDrUOnlgw8PYpCp31KvQ6vV1j";
        String iv = DigestUtils.md5Hex("htznhhakDrUOnlgw8PYpCp31KvQ6vV1j").substring(0,16);
        System.out.println("明文：" + content);
        System.out.println("key：" + password);
        try {
            String encryptResult = encrypt("htznhhakDrUOnlgw8PYpCp31KvQ6vV1j", "a0b5cd09aca79e6d", "success");
            System.out.println("密文：" + encryptResult);
            String decryptResult = decrypt("htznhhakDrUOnlgw8PYpCp31KvQ6vV1j", "a0b5cd09aca79e6d", encryptResult);
            System.out.println("解密：" + decryptResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
